
/*
 *  Copyright 2007 Marc Lanctot and Marc Gendron-Bellemare
 *
 *  This file is part of OpenMTG.
 *
 *  OpenMTG is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  OpenMTG is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with OpenMTG.  If not, see <http://www.gnu.org/licenses/>.
 *
 */


import java.util.*;

public class CombatGroup
{
	public ArrayList<Card> aMembers;
	protected int aDamage;
	protected int aOwner;
  protected int aDamageOwner;

	public CombatGroup(int pOwner)
	{
		aMembers = new ArrayList<Card>();
		aOwner = pOwner;
	}

	public CombatGroup(Card c, int pOwner)
	{
		this(pOwner);
		aMembers.add(c);
	}

	public int getPower()
	{
		int power = 0;
		for (Card c : aMembers)
		{
			power += c.getPower();
		}

		return power;
	}

	public void addDamage (int pDamage) { aDamage += pDamage; }
	public int getDamage() { return aDamage; }

	public int getOwner() { return aOwner; }

  public int getDamageOwner() { return aDamageOwner; }
  public void setDamageOwner(int pid) { aDamageOwner = pid; }

	/** Assigns all accumulated damage to the first card. */
	public Card assignDamage()
	{
		if (aMembers.size() > 0)
		{
			Card sucker = aMembers.get(0);
			assignDamage(sucker, aDamage);
			return sucker;
		}
		else
			return null;
	}
	
	public boolean assignDamage(Card c, int pDamage)
	{
		boolean found = false;

		if (pDamage > aDamage) return false; // No cheating!
		for (Card mem : aMembers)
		{
			if (mem.getCID() == c.getCID())
			{
				found = true;
				break;
			}
		}

		if (!found) return false;
		
		c.addDamage(pDamage);
		aDamage -= pDamage;
		return true;
	}

  public Card getCard(int iid)
  {
    if (aMembers == null) return null;

    for (Card c : aMembers)
    {
      if (c.getIID() == iid) return c;
    }

    return null;
  }
}
	

